#include"MyGraphVisual.h"
#include<iostream>


GraphVisual::GraphVisual(std::vector<CPoint> cp, std::vector<edgeVisual> ce) {
	CedgeAllVisual.RemoveAll();
	CnodeVisual.RemoveAll();
	//this->CEdge_all = Edge_all;
	for (int i = 0; i < ce.size(); i++) {
		CedgeAllVisual.Add(ce[i]);
	}
	//this->CNode = Node;
	for (int i = 0; i < cp.size(); i++) {
		CnodeVisual.Add(cp[i]);
	}
}

void GraphVisual::create() {
	for (int i = 1; i <= maxlen; i++) {
		vertex[i - 1].in = 0;
		vertex[i - 1].out = 0;
	}
	parentof = new int;

	int node_num, edge_num;
	node_num = CnodeVisual.GetSize();
	currentvertex = node_num;
	edge_num = CedgeAllVisual.GetSize();

	for (int i = 1; i <= node_num; i++) {
		vertex[i - 1].data = i - 1;
		vertex[i - 1].color = -1;
		vertex[i - 1].next = NULL;
		vertex[i - 1].rear = &vertex[i - 1];
	}

	for (int i = 1; i <= edge_num; i++) {
		int edge_start, edge_end;
		edge_start = CedgeAllVisual[i - 1].start;
		edge_end = CedgeAllVisual[i - 1].end;
		int ind1 = index(edge_start);
		int ind2 = index(edge_end);
		adjlink* left = new adjlink;
		adjlink* right = new adjlink;
		left->data = edge_start;
		right->data = edge_end;
		vertex[ind1 - 1].rear->next = right;
		vertex[ind1 - 1].rear = right;
		vertex[ind1 - 1].rear->next = NULL;
		vertex[ind2 - 1].rear->next = left;
		vertex[ind2 - 1].rear = left;
		vertex[ind2 - 1].rear->next = NULL;
		

	}
}

CString GraphVisual::showgraph() {
	CString cs;
	CString cstmp;
	for (int i = 1; i <= currentvertex; i++) {
		adjlink* a = &vertex[i - 1];
		cstmp.Format(TEXT("%d"), vertex[i - 1].data);
		cs += cstmp;
		std::cout << vertex[i - 1].data;
		a = a->next;
		while (a != NULL) {
			std::cout << "->";
			cs += "->";
			std::cout << a->data;
			cstmp.Format(TEXT("%d"), a->data);
			cs += cstmp;
			a = a->next;
		}
		std::cout << std::endl;
		cs += '\n';
	}
	return cs;
}

std::vector<int> GraphVisual::showcolor() {
	std::vector<int> colorArray;
	for (int i = 0; i < currentvertex; i++)
		colorArray.push_back(vertex[i].color);
	return colorArray;
}